package com.deyuanyun.pic.pbf.controller.dto.validata;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.RequestResponseContext;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.pbf.controller.dto.DefectInfoDTO;
import com.deyuanyun.pic.pbf.service.RepairTaskService;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;

/**
 * 验证缺陷点是否存在
 * 
 * @author ChenKui
 * @date 2016-02-22
 */
public class DefectInfoValidata implements ValidateInterface {

    protected Logger logger = LogManager.getLogger(this.getClass());
    
	@Override
	public <T, E,C> boolean apply(T t, E e,C c) {
		if(ObjectUtil.isNotNullEmpty(t)){
		    String defectInfoCode = (String)t;
		    RepairTaskService repairTaskService = (RepairTaskService)SpringAppliction.getBean(RepairTaskService.class);
		    DefectInfoDTO defectInfo = repairTaskService.queryDefectInfoByCustomNumber(defectInfoCode);//2016-10-27专门修改为了自定义编号验证
		    if(defectInfo == null){
		        logger.debug("缺陷点不存在：" + defectInfoCode);
		        return false;
		    }
		    HttpServletRequest request = RequestResponseContext.getRequest();
            String lineId_request = request.getParameter("pipeLineId");
            String fromPointId = request.getParameter("fromPointId");
            String toPointId = request.getParameter("toPointId");
            
		    BigDecimal mileage = defectInfo.getMileage();
		    PipeLocationService pipeLocationService = (PipeLocationService)SpringAppliction.getBean(PipeLocationService.class);
		    
		    PipeLocationDTO pipeLocation1 = pipeLocationService.queryPipeLocationById(lineId_request, fromPointId);
		    PipeLocationDTO pipeLocation2 = pipeLocationService.queryPipeLocationById(lineId_request, toPointId);
		    
		    if(mileage.compareTo(pipeLocation1.getMileage()) == -1 || mileage.compareTo(pipeLocation2.getMileage()) > 0){
		        logger.debug("缺陷点不在所选定的绝对距离内：" );
		        return false;//
		    }
		    
		    if(StringUtils.isEmpty(defectInfo.getRepairTaskId())){
                
                 
		        return defectInfo.getPipeId().equals(lineId_request);
		    }else{
		        logger.debug("缺陷点已加入别的任务：" + defectInfoCode + "、" + defectInfo.getRepairTaskId());
		        return false;
		    }
			
		}
		return false;
	}

	@Override
	public String getName() {
		return null;
	}

}
